package com.brisk.web.controller.applet;

import com.ruoyi.applet.domain.WechatUser;
import com.ruoyi.applet.service.IWechatUserService;
import com.brisk.common.annotation.Log;
import com.brisk.common.core.controller.BaseController;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.core.page.TableDataInfo;
import com.brisk.common.enums.BusinessType;
import com.brisk.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * weChatUserController
 *
 * @author PengFei
 * @date 2022-05-31
 */
@RestController
@RequestMapping("/applet/weChatUser")
public class WechatUserController extends BaseController {
    @Autowired
    private IWechatUserService wechatUserService;

    /**
     * 查询weChatUser列表
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatUser wechatUser) {
        startPage();
        List<WechatUser> list = wechatUserService.selectWechatUserList(wechatUser);
        return getDataTable(list);
    }

    /**
     * 导出weChatUser列表
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:export')")
    @Log(title = "weChatUser", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WechatUser wechatUser) {
        List<WechatUser> list = wechatUserService.selectWechatUserList(wechatUser);
        ExcelUtil<WechatUser> util = new ExcelUtil<WechatUser>(WechatUser.class);
        util.exportExcel(response, list, "weChatUser数据");
    }

    /**
     * 获取weChatUser详细信息
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(wechatUserService.selectWechatUserByUserId(userId));
    }

    /**
     * 新增weChatUser
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:add')")
    @Log(title = "weChatUser", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WechatUser wechatUser) {
        return toAjax(wechatUserService.insertWechatUser(wechatUser));
    }

    /**
     * 修改weChatUser
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:edit')")
    @Log(title = "weChatUser", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WechatUser wechatUser) {
        return toAjax(wechatUserService.updateWechatUser(wechatUser));
    }

    /**
     * 删除weChatUser
     */
    @PreAuthorize("@ss.hasPermi('applet:weChatUser:remove')")
    @Log(title = "weChatUser", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(wechatUserService.deleteWechatUserByUserIds(userIds));
    }


    /**
     * 绘画echarts 排行榜
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PengFei Deng
     * @date 2022/6/3 1:44
     */
    @GetMapping("getUserRankInfo")
    public AjaxResult getUserRankInfo() {
        List<WechatUser> rankList = wechatUserService.selectWechatRankList(new WechatUser());
        List<String> xData = new ArrayList<>(); // 横坐标 纵坐标
        List<BigDecimal> mileageSum = new ArrayList<>();
        List<Integer> daySum = new ArrayList<>();
        int size = Math.min(rankList.size(), 10);
        for (int i = 0; i < size; i++) {
            WechatUser temp = rankList.get(i);
            xData.add(temp.getUserName() == null ? temp.getUserNickname() : temp.getUserName());
            mileageSum.add(temp.getMileage());
            daySum.add(temp.getDaySum());
        }
        Map<String, Object> res = new HashMap<>();
        res.put("xData", xData);
        res.put("mileageSum", mileageSum);
        res.put("daySum", daySum);
        return AjaxResult.success(res);

    }


}
